package com.demo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basic.domain.PageResult;
import com.basic.util.ConverterUtil;
import com.basic.util.QueryHelpPlus;
import com.demo.mapper.UserMapper;
import com.demo.pojo.User;
import com.demo.service.IUserService;
import com.demo.service.criteria.UserQueryCriteria;
import com.demo.service.dto.UserDto;
import com.demo.service.vo.MyConverterUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cjy
 * @since 2024-10-10
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public UserDto saveOrUpdate(UserDto userDto) {
        final User user = ConverterUtil.convert(userDto, User.class);
        if (ObjectUtil.isNull(user.getId())) {
            baseMapper.insert(user);
        } else {
            baseMapper.updateById(user);
        }
        return convertDto(user, null);
    }

    @Override
    public List<UserDto> queryAll(UserQueryCriteria criteria) {
        return convertDto(baseMapper.selectList(query(criteria)), criteria);
    }

    @Override
    public PageResult<UserDto> queryAll(UserQueryCriteria criteria, Pageable pageable) {
        final int page;
        final int limit;
        if (criteria.getPage() != null && criteria.getLimit() != null) {
            page = criteria.getPage();
            limit = criteria.getLimit();
        } else {
            page = pageable.getPageNumber();
            limit = pageable.getPageSize();
        }
        final IPage<User> pageList = baseMapper.selectPage(new Page<>(page, limit), query(criteria));

        return new PageResult<UserDto>(pageList.getTotal(), convertDto(pageList.getRecords(), criteria));
    }

    @Override
    public UserDto queryById(Long id) {
        final List<UserDto> records = queryAll(UserQueryCriteria.builder().id(id).build());
        if (!records.isEmpty()) {
            return records.get(0);
        }
        return null;
    }

    @Override
    public UserDto queryOne(UserQueryCriteria criteria) {
        final List<UserDto> records = queryAll(criteria);
        if (!records.isEmpty()) {
            return records.get(0);
        }
        return null;
    }

    @Override
    public Long count(UserQueryCriteria criteria) {
        return baseMapper.selectCount(query(criteria));
    }

    @Override
    public User getOne(UserQueryCriteria criteria) {
        final List<User> records = getAll(criteria);
        if (!records.isEmpty()) {
            return records.get(0);
        }
        return null;
    }

    @Override
    public List<User> getAll(UserQueryCriteria criteria) {
        return baseMapper.selectList(query(criteria));
    }

    @Override
    public int deleteId(Long id) {
        final UserDto userDto = queryById(id);
        delete(userDto);
        return 1;
    }

    @Override
    @Transactional
    public int deleteByMore(UserQueryCriteria criteria) {
        final List<UserDto> userDtos = queryAll(criteria);
        for (UserDto each: userDtos) {
            delete(each);
        }
        return userDtos.size();
    }

    private void delete(UserDto userDto) {
        userDto.setIsShow(0);
        saveOrUpdate(userDto);
    }

    @Override
    public List<User> testXml() {

        return userMapper.selectAllData();
    }

    /**
     * 后续所有的sql逻辑都在这里处理
     */
    private QueryWrapper<User> query(UserQueryCriteria criteria) {
        final QueryWrapper<User> wrapper = QueryHelpPlus.getPredicate(User.class, criteria);
        if (ObjectUtil.isNull(criteria)) {
            return wrapper;
        }

        if (ObjectUtil.isNull(criteria.getIsShow())) {
            wrapper.eq("is_show", 1);
        }

        return wrapper;
    }

    private UserDto convertDto(User record, UserQueryCriteria criteria) {
        return MyConverterUtils.INSTANCE.toUserDto(record);
    }

    private List<UserDto> convertDto(List<User> records, UserQueryCriteria criteria) {

        final List<UserDto> result = new ArrayList<>();

        for (User each : records) {
            result.add(convertDto(each, criteria));
        }

        return result;
    }
}
